﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BySoft.Server.Common.Enum;
using BySoft.Server.Business;
using BySoft.Web.Utility;
using BySoft.Server.Common.Entity;
using BySoft.Server.Consts;
using BySoft.Server.Common;
using System.Data;
using OneSoft.EntityCore;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.IO;
using System.Text;

namespace BySoft.Web.Site.ComPage
{
    public partial class SetOutCheck : BasePopPage
    {
        #region 变量定义
       
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                    hidDetailID.Value = UIHelper.GetQueryStringValue("id");
                InitData(hidDetailID.Value);
            }
        }

        void InitData(string strCode)
        {
       

            BsOutbackmatEntity entity = BsOutbackmat.GetBsOutbackmatByID(strCode);
            lblReceiveNum.Text =entity.Nums.ToString();

            lblQualifiedNum.Text = lblReceiveNum.Text;
            plUnQualifiedNum.Visible = false;

        }


        protected void ddlResultType_Changed(object sender, EventArgs e)
        {
            try
            {
                plUnQualifiedNum.Visible = false;
                lblQualifiedNum.Text = lblReceiveNum.Text;
                 txtUnQualifiedNum.Text = "";
              
                txtUnqualifiedReason.Text = "";
                if (ddlResultType.SelectedValue == "3")
                {
                     plUnQualifiedNum.Visible = false;
                }
                else if (ddlResultType.SelectedValue == "5")
                {
                   
                    plUnQualifiedNum.Visible = true;
                }
            }
            catch (Exception ex)
            {
                UIHelper.AlertMessage(this.Page, ex.ToString());
            }
        }

        protected void btnOk_Click(object sender, EventArgs e)
        {
            try
            {
                if (!ValidateData())
                {
                    return;
                }

                string strBillID = CommonMethod.GetString(hidDetailID.Value.Trim());
              
                         BsOutbackmatEntity entity = BsOutbackmat.GetBsOutbackmatByID(strBillID);

                          string strCheckStatus = CommonMethod.GetString(ddlResultType.SelectedValue.Trim()); 
                        entity.CheckResult= strCheckStatus; 
                        if (strCheckStatus == "1")
                        {
                            entity.UnqualifyNum = 0;
                          
                            entity.UnqualifyReason ="";
                           
                        }
                        else if (strCheckStatus == "5")
                        {
                            entity.UnqualifyNum = CommonMethod.ConvertToDecimal(txtUnQualifiedNum.Text.Trim());
                         
                            entity.UnqualifyReason = CommonMethod.GetString(txtUnqualifiedReason.Text.Trim());
                       
                        }

                        BsOutbackmat.SaveOrUpdate(entity, false);

               
                string sReturn = "success";
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>setCookie(dialogReturn_key,'" + sReturn + "',1);CloseTopDialog();</script>");
            }
            catch (Exception ex)
            {
                UIHelper.AlertMessage(this.Page, ex.Message.ToString());
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            try
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>CloseTopDialog();</script>");
            }
            catch (Exception ex)
            {
                UIHelper.Alert(this.Page, ex.ToString().Trim());
            }

        }

        private bool ValidateData()
        {
            decimal dRecieve = CommonMethod.ConvertToDecimal(lblReceiveNum.Text);
             decimal dUnQualifiedNum = CommonMethod.ConvertToDecimal(txtUnQualifiedNum.Text);
            string strMsg = "";
            if (string.IsNullOrEmpty(ddlResultType.SelectedValue))
            {
                UIHelper.AlertMessage(this.Page, "请选择检验结果！");
                return false;
            }
           
            if (ddlResultType.SelectedValue == "5")
            {
                lblQualifiedNum.Text = (dRecieve - dUnQualifiedNum).ToString();
                if (dUnQualifiedNum<=0)
                {
                    UIHelper.AlertMessage(this.Page, "请填写不合格数量！");
                    return false;
                }

                if (string.IsNullOrEmpty(txtUnqualifiedReason.Text)){
                    UIHelper.AlertMessage(this.Page, "请填写不合格原因！");
                    return false;
                }
            }

            return true;
        }
    }
}